package com.zx.test.core.mock;


import com.zx.flow.api.dict.FlowDict.ExecuteStatus;
import com.zx.flow.api.dict.FlowDict.Status;
import com.zx.flow.api.dict.FlowDict.StorageType;
import com.zx.flow.api.execute.model.FlowContext;
import com.zx.flow.api.execute.model.FlowOperator;
import com.zx.flow.api.execute.response.FlowResult;
import com.zx.flow.core.engine.FlowEngineImpl;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;

@Slf4j
public class Common {

    public static FlowEngineImpl flowEngine;
    static String user1 = Common.user1().getAccountCode();
    static String user3 = Common.user3().getAccountCode();
    static String user4 = Common.user4().getAccountCode();
    static String user5 = Common.user5().getAccountCode();
    static String user6 = Common.user6().getAccountCode();
    private static boolean isInit = false;

    public static void init() throws Exception {
        if (isInit) {
            return;
        }
        flowEngine = new FlowEngineImpl();
        flowEngine.setStorageType(StorageType.mock);
        flowEngine.init();
        isInit = true;
    }

    public static void check(FlowResult result, Integer nodeNum, Status status,
            String currentNodeCode) throws Exception {
        FlowContext flow = result.getFlowContext();
        Assert.assertEquals(result.getStatus(), ExecuteStatus.success);
        if (nodeNum != null) {
            Assert.assertEquals(flow.getNodes().size(), nodeNum.intValue());
        }
        if (status != null) {
            Assert.assertEquals(flow.getStatus(), status);
        }
        if (currentNodeCode != null) {
            Assert.assertEquals(flow.getCurrentNodeCode(), currentNodeCode);
        }
    }

    public static void check(FlowContext flow, Integer nodeNum, Status status,
            String currentNodeCode) throws Exception {
        if (nodeNum != null) {
            Assert.assertEquals(flow.getNodes().size(), nodeNum.intValue());
        }
        if (status != null) {
            Assert.assertEquals(flow.getStatus(), status);
        }
        if (currentNodeCode != null) {
            Assert.assertEquals(flow.getCurrentNodeCode(), currentNodeCode);
        }
    }

    public static FlowOperator user1() {
        return new FlowOperator("a1", "阿一", null);
    }

    public static FlowOperator user3() {
        return new FlowOperator("zhangsan", "张三", null);
    }

    public static FlowOperator user4() {
        return new FlowOperator("lisi", "李四", null);
    }

    public static FlowOperator user5() {
        return new FlowOperator("wangWu", "王五", null);
    }

    public static FlowOperator user6() {
        return new FlowOperator("zhaoLiu", "赵六", null);
    }

}
